package com.hellobike.haxing.modules.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.oConvertUtils;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.product.TreeNode;
import com.hellobike.haxing.modules.product.entity.Category;
import com.hellobike.haxing.modules.product.exception.ProductException;
import com.hellobike.haxing.modules.product.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 后台分类
 * @Author: haxing-boot
 * @Date: 2020-01-11
 * @Version: V1.0
 */
@RestController
@RequestMapping("/product/category")
@Slf4j
public class CategoryController extends BaseController<Category, ICategoryService> {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 分页列表查询
     *
     * @param category
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "后台分类-分页列表查询")
    @GetMapping(value = "/rootList")
    public Result<?> queryPageList(Category category,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (oConvertUtils.isEmpty(category.getParentId())) {
            category.setParentId(0L);
        }
        QueryWrapper<Category> queryWrapper = QueryGenerator.initQueryWrapper(category, req.getParameterMap());
        queryWrapper.lambda().orderByAsc(Category::getSerial);
        Page<Category> page = new Page<Category>(pageNo, pageSize);
        IPage<Category> pageList = categoryService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 获取子数据
     *
     * @param req
     * @return
     */
    @GetMapping(value = "/childList")
    public Result<?> queryPageList(Category category, HttpServletRequest req) {
        if (category.getParentId() == null) {
            return Result.ok(new ArrayList<>());
        }
        QueryWrapper<Category> queryWrapper = QueryGenerator.initQueryWrapper(category, req.getParameterMap());
        queryWrapper.lambda().orderByAsc(Category::getSerial);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.ok(list);
    }

    /**
     * 获取完整类目树
     *
     * @return
     */
    @GetMapping("/allTree")
    public Result<?> getAllTree() {
        List<TreeNode> allTree = categoryService.getAllTree();
        return Result.ok(allTree);
    }


    /**
     * 添加
     *
     * @param category
     * @return
     */
    @AutoLog(value = "后台分类-添加", operateType = 2)
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Category category) {
        try {
            categoryService.addCategory(category);
            return Result.ok("添加成功！");
        } catch (ProductException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 编辑
     *
     * @param category
     * @return
     */
    @AutoLog(value = "后台分类-编辑", operateType = 3)
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody Category category) {
        try {
            categoryService.updateCategory(category);
            return Result.ok("编辑成功!");
        } catch (ProductException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "后台分类-删除", operateType = 4)
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) Long id) {
        categoryService.deleteCategory(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "后台分类-批量删除", operateType = 4)
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.categoryService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Long id) {
        Category category = categoryService.getById(id);
        if (category == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(category);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param category
     */
    @AutoLog(value = "后台分类-导出excel", operateType = 6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Category category) {
        return super.exportXls(request, category, Category.class, "后台分类");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "后台分类-导入excel", operateType = 5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Category.class);
    }

}
